package com.ce.pms.report.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ce.pms.report.dao.IReportDao;
import com.ce.pms.report.queryvo.GrantBuildAndItemQueryVO;
import com.ce.pms.report.vo.GrantBuildAndItemReportVO;
import com.ce.report.entity.CEReportDataSet;
import com.ce.report.exception.CEReportException;
import com.ce.report.service.ICEReportDataSetService;
import com.ce.report.vo.ReportsQueryBaseVo;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;

/**
 *
 * 功能描述：财务应收款分栋分项汇总表 Service
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 黄彦军 Created on 2008-10-29 Edit author: Edit on: 修改原因:
 */
public class GrantBuildAndItemService implements
		ICEReportDataSetService {
	private IReportDao reportDao;

	public CEReportDataSet getReportDataSet(ReportsQueryBaseVo arg) throws CEReportException {
		GrantBuildAndItemQueryVO queryVO = (GrantBuildAndItemQueryVO) arg;

		return new CEReportDataSet(getData(queryVO),getHeadInfo(queryVO));
	}

	/**
	 * 功能描述:获取报表数据 <br>
	 * @param queryVO
	 * @return Create author:黄彦军 <br>
	 *         Create on:2008-11-6 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private  List getData(GrantBuildAndItemQueryVO queryVO) {

		List<Object[]> temp = reportDao.getGrantBuildAndItemList(queryVO);
		List<GrantBuildAndItemReportVO> list = arrayToVO(temp);

		return list;
	}

	/**
	 * 功能描述:将对象数组 转为 VO 数组 <br>
	 *
	 * @param temp
	 * @return Create author:黄彦军 <br>
	 *         Create on:2008-11-7 <br>
	 *         Edit author: <br>
	 *         Edit on: <br>
	 *         Why: <br>
	 */
	private List<GrantBuildAndItemReportVO> arrayToVO(
			List<Object[]> temp) {
		if (BlankUtil.isBlank(temp)) {
			return null;
		}
		List<GrantBuildAndItemReportVO> list = new ArrayList<GrantBuildAndItemReportVO>();
		for (Object[] obj : temp) {
			GrantBuildAndItemReportVO reportVO = new GrantBuildAndItemReportVO();

			reportVO.setCompanyName((String) obj[0]); // 物业名称
			reportVO.setBuildingName((String) obj[1]); // 大楼名称
			reportVO.setAccountReceivable(setValue(obj[2])); // 本月应收合计
			reportVO.setGrantCount(obj[3] == null ? null : (new Integer(obj[3].toString()))); 					// 应收户数
			reportVO.setActualThis(setValue(obj[4])); 		// 本月费用
			reportVO.setArrearagePay(setValue(obj[5])); 		// 往月欠款
			reportVO.setLatefee(setValue(obj[6])); 			// 滞纳金
			reportVO.setOverheadExpenses(setValue(obj[7])); 	// 管理费
			reportVO.setOnticUpkeep(setValue(obj[8])); 		// 本体维修基金
			reportVO.setElectricityPay(setValue(obj[9])); 	// 电费
			reportVO.setWaterPay(setValue(obj[10])); 		// 水费
			reportVO.setSewagePay(setValue(obj[11])); 		// 污水处理费
			reportVO.setGarbagePay(setValue(obj[12])); 		// 垃圾处理费
			reportVO.setUpkeepPay(setValue(obj[13])); 		// 维修费

			list.add(reportVO);
		}
		return list;
	}


//	传入的值必须是 BigDecimal 类型的,否则返回空如果为空或值为0 则返回空
	private BigDecimal setValue(Object obj){
		if (obj instanceof BigDecimal) {
			if(((BigDecimal)obj).intValue() == 0){
				return null;
			}
			return (BigDecimal)obj;
		}
		return null;
	}

	/**
	 * 功能描述:获取头信息  <br>
	 * @param queryVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Map<String, Object> getHeadInfo(GrantBuildAndItemQueryVO queryVO){
		Map<String, Object> map =new HashMap<String, Object>();
		String computeMonth = DateUtil.convertDateToStr(queryVO.getComputeMonth(), "yyyy年MM月份");
		map.put("reportHeadStr",computeMonth);
		map.put("printDate", new Date());
		String subentry = "";
		if("1".equals(queryVO.getSubentry())){
			subentry="本月费用";
		}else if("2".equals(queryVO.getSubentry())){
			subentry="往月欠款";
		}else{
			subentry="本月费用 + 往月欠款";
		}
		map.put("subentry", subentry);
		return map;
	}

	public void setReportDao(IReportDao reportDao) {
		this.reportDao = reportDao;
	}

}
